﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;
using Telerik.Web.UI;
using System.Data;

namespace Web.Loyalty
{
    public partial class Loyalty : System.Web.UI.Page
    {
        static USER user;
        static List<LOYALTY> loyals;
        static long? assignedtoID = null;
        static ROLE r;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            assignedtoID = null;
            user = (USER)Session["User"];
            if (user != null)
            {
                DateTime dt = DateTime.Now;
                DateTime dt2 = dt.AddMonths(-3);
                dpDateCreatedFrom.SelectedDate = new DateTime(dt2.Year, dt2.Month, 1);
                dpDateCreatedTo.SelectedDate = DateTime.Now;
                LoadAllUser();
                r = RoleService.GetRoleByID((long)user.ROLE_ID);
                if ((!(bool)user.IS_ADMIN) && (!(bool)r.IS_MANAGER))
                {
              //      assignedtoID = user.ID;
                    divAssignedTo.Visible = false;
                    divAddLoyalty.Visible = false;
                }
                loyals = getDataSource(dpDateCreatedFrom.SelectedDate, dpDateCreatedTo.SelectedDate.Value.AddHours(24), txtLoyaltyName.Text.Trim(), rntxtPointToAward.Value, rntxtExchangeRate.Value, assignedtoID);
                RadGrid1.DataSource = loyals;
                RadGrid1.DataBind();
                
            }
        }
        private List<LOYALTY> getDataSource(DateTime? dateFrom, DateTime? dateTo, string name, double? pointToAward, double? exchangeRate, long? assignedtoID)
        {
            List<LOYALTY> dt = LoyaltyService.GetLoyaltyByCondition(dateFrom, dateTo, name, pointToAward == 0 ? null : pointToAward, exchangeRate == 0 ? null : exchangeRate, assignedtoID);
            if (dt == null)
            {
                dt = new List<LOYALTY>();
            }
            return dt;
        }
        private void LoadAllUser()
        {
            ddlAssignedTo.Items.Clear();
            List<USER> users = UserService.ListAll();
            if (users != null)
            {
                foreach (USER u in users)
                {
                    ddlAssignedTo.Items.Add(new RadComboBoxItem(u.SUR_NAME + " " + u.NAME, u.ID.ToString()));
                }
            }
            ddlAssignedTo.Items.Insert(0, new RadComboBoxItem("Select an option", "-1"));
        }
        protected void btnAddLoyalty_ServerClick(object sender, EventArgs e)
        {
            Response.Redirect("NewLoyalty.aspx");
        }

        protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            RadGrid1.DataSource = loyals;
        }

        protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            
            if ((!(bool)user.IS_ADMIN) && (!(bool)r.IS_MANAGER))
            {
                RadGrid1.MasterTableView.GetColumn("ASSIGN_TO_NAME").Visible = false;
                RadGrid1.MasterTableView.GetColumn("TOOLS").Visible = false;
                RadGrid1.MasterTableView.GetColumn("NAME").ItemStyle.Width = Unit.Percentage(25);
                RadGrid1.MasterTableView.GetColumn("NAME").HeaderStyle.Width = Unit.Percentage(25);
                RadGrid1.MasterTableView.GetColumn("DESCRIPTION").ItemStyle.Width = Unit.Percentage(30);
                RadGrid1.MasterTableView.GetColumn("DESCRIPTION").HeaderStyle.Width = Unit.Percentage(30);
            }
        }

        protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridDataItem dataItem = e.Item as GridDataItem;
            switch (e.CommandName)
            {
                case "Edit":
                    Response.Redirect("EditLoyalty.aspx?id=" + dataItem.GetDataKeyValue("ID").ToString());
                    break;
                case "Delete":
                    long loID = Convert.ToInt64(dataItem.GetDataKeyValue("ID").ToString());
                    loyals.Remove(loyals.Where(x => x.ID == loID).FirstOrDefault<LOYALTY>());
                    int deleteLo = LoyaltyService.DeleteLoyalty(loID);
                    if (deleteLo == 1)
                         RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Delete loyalty successful !" + "');");
                    else
                        RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Can not delete loyalty !" + "');");
                    break;
                default:
                    break;
            }
        }

        protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridPagerItem)
            {
                GridPagerItem oGridPagerItem = (GridPagerItem)e.Item;
                RadComboBox oRadComboBox = (RadComboBox)oGridPagerItem.FindControl("PageSizeComboBox");
                oRadComboBox.Width = Unit.Pixel(50);
            }
        }

        protected void btnSearch_ServerClick(object sender, EventArgs e)
        {
            if (divAssignedTo.Visible)
            {
                long? at = Convert.ToInt64(ddlAssignedTo.SelectedValue);
                assignedtoID = at == -1 ? null : at;
            }
            loyals = getDataSource(dpDateCreatedFrom.SelectedDate, dpDateCreatedTo.SelectedDate.Value.AddHours(24), txtLoyaltyName.Text.Trim(), rntxtPointToAward.Value, rntxtExchangeRate.Value, assignedtoID);
            RadGrid1.DataSource = loyals;
            RadGrid1.DataBind();
        }
    }
}